 /* ---------------------------------------------------------------------- *
 *																		   *
 *	This code is part of FotoKinect, Spring 2011 by Gianna Badiali,		   *
 *                                                   Gabriel Fernandez,	   *
 *                                                   Kelsey Tripp, and     *
 *                                                   Sam Swarr             *
 *																		   *
 ***************************************************************************/

#include "HelpWidget.h"
#include "ui_HelpWidget.h"
#include <QWidget>
#include <iostream>
#include <QStateMachine>
#include <QPropertyAnimation>
#include <QSignalTransition>
#include <QFrame>

HelpWidget::HelpWidget(QWidget *parent) :
QWidget(parent) ,
	ui(new Ui::HelpWidget)
{
	ui->setupUi(this);

	QStateMachine * machine = new QStateMachine();

	QState *introState = new QState(machine);
	hideIndex(introState);
	hideHelpContent(introState);
	introState->assignProperty(ui->introText, "geometry", QRect(180, 140, ui->introText->width(), ui->introText->height()));
	ui->introText->setHtml("<font size=75><center><b><font color=""black"">Welcome to FotoKinect!</font></b><br><br> Use your hands to create brilliant works of art. <br><br>Wave a hand to sync a cursor. " 
		"Bring a second hand close to the first and wave again to sync a second cursor.  To select, push your hand "
		"forward.  If you've correctly selected, the cursor will turn <font color=""green""><b>green</b></font>. If you try "
		"to move off of the canvas, or perform a gesture that is not allowed, the cursors will turn <font color=""red""><b>red</b></font>. "
		"If you lose a cursor, simply wave again to get it back.<br><br>Use your hands to move, rotate, resize, and skew photos. "
		"Have fun exploring all of the possible gestures...<br><br><b>Welcome to the world of natural interaction!</b></font></center>");

	QState *backgroundState = new QState(machine);
	hideIndex(backgroundState);
	hideHelpContent(backgroundState);
	backgroundState->assignProperty(ui->backgroundText, "geometry", QRect(180, 140, ui->backgroundText->width(), ui->backgroundText->height()));
	ui->backgroundText->setHtml("<font size=75><center><br><br>To change the background color of the canvas, select the <b>Canvas</b> button from the menu.<br><br>This will open "
		"a hue selector on the left and a value selector on the right.  Move the cursors on top of either selector "
		"to preview that color. <br><br>To confirm your color choice, perform the select gesture. <br><br>To cancel your selection and "
		"revert to the previous canvas color, select the cancel button in the top left corner. You can also leave color mode "
		"mode by selecting either the menu button or the help button.</font></center>");

	QState *cropState = new QState(machine);
	hideIndex(cropState);
	hideHelpContent(cropState);
	cropState->assignProperty(ui->cropText, "geometry", QRect(180, 140, ui->cropText->width(), ui->cropText->height()));
	ui->cropText->setHtml("<font size=75><center><br>To crop a photo, select the <b>Crop</b> button from the menu.  <br><br>If there are no photos currently on the canvas, "
		"the cursor will flash red. Otherwise, you will be asked to select one of the photos from the canvas to crop. <br><br>"
		"Once you select the photo, an enlarged version will appear.  Move the cursors to manipulate the crop selection rectangle. "
		"<br><br>To confirm the crop, perform the select gesture. To cancel the crop, select the cancel button in the top left corner. "
		"You can also leave crop mode by selecting either the menu button or the help button.</center></font>");

	QState *eraseState = new QState(machine);
	hideIndex(eraseState);
	hideHelpContent(eraseState);
	eraseState->assignProperty(ui->eraseText, "geometry", QRect(180, 140, ui->eraseText->width(), ui->eraseText->height()));
	ui->eraseText->setHtml("<font size=75><center><br><br><br><br><br>To remove a photo from the canvas, select the photo you wish to delete with your left cursor, "
		"and select the <b>Trash</b> button at the bottom right corner of the screen.</center></font>");

	QState *exportState = new QState(machine);
	hideIndex(exportState);
	hideHelpContent(exportState);
	exportState->assignProperty(ui->exportText, "geometry", QRect(180, 140, ui->exportText->width(), ui->exportText->height()));
	ui->exportText->setHtml("<font size=75><center><br><br>To export your FotoKinect collage as a PNG file, select the <b>Export</b> button from the menu. "
		"<br><br>Your file will be saved in <b>C:\\Users\\Public\\Pictures\\FotoKinect\\Exported Images</b>, with the "
		"filename as the current timestamp.</font></center>");

	QState *flipState = new QState(machine);
	hideIndex(flipState);
	hideHelpContent(flipState);
	flipState->assignProperty(ui->flipText, "geometry", QRect(180, 140, ui->flipText->width(), ui->flipText->height()));
	ui->flipText->setHtml("");

	QState *importState = new QState(machine);
	hideIndex(importState);
	hideHelpContent(importState);
	importState->assignProperty(ui->importText, "geometry", QRect(180, 140, ui->importText->width(), ui->importText->height()));
	ui->importText->setHtml("<font size=75><center><br><br>To add a photo to the canvas, select the <b>Import</b> button from the menu.<br><br>  A photo browser "
		"will appear displaying the contents of your photo library, located at <b>C:\\Users\\Public\\Pictures\\FotoKinect\\Pictures To Import</b>. "
		"Simply add your own images to this folder in order to import them into FotoKinect.<br><br> To cycle through adjacent "
		"photos, perform a swipe gesture by moving a cursor from the center of the screen to either side.<br><br>  Once you have "
		"found the picture you wish to import, simply perform the select gesture to import it.</center></font>");

	QState *loadState = new QState(machine);
	hideIndex(loadState);
	hideHelpContent(loadState);
	loadState->assignProperty(ui->loadText, "geometry", QRect(180, 140, ui->loadText->width(), ui->loadText->height()));
	ui->loadText->setHtml("<font size=75><center><br><br>To load a saved FotoKinect project, select the <b>Load</b> button from the menu.<br><br>  A file browser will appear "
		"displaying the contents of your FotoKinect projects folder, located at <b>C:\\Users\\Public\\Pictures\\FotoKinect\\Projects</b>. "
		"<br><br>To cycle through your projects, which are represented by the date and time that they were saved, perform a "
		"swipe gesture by moving a cursor from the center of the screen to either side. <br><br>Once you have found the project that "
		"you wish to load, simply perform the select gesture to load it.</center></font>");

	QState *moveState = new QState(machine);
	hideIndex(moveState);
	hideHelpContent(moveState);
	moveState->assignProperty(ui->moveText, "geometry", QRect(180, 140, ui->moveText->width(), ui->moveText->height()));
	ui->moveText->setHtml("<font size=75><center><br><br><br><br><br>To move a photo around the canvas, select the photo with one cursor.  <br><br>This will grab the "
		"photo, which can now be panned around the screen. To exit move mode, deselect the photo.</center></font>");

	QState *reorderState = new QState(machine);
	hideIndex(reorderState);
	hideHelpContent(reorderState);
	reorderState->assignProperty(ui->reorderText, "geometry", QRect(180, 140, ui->reorderText->width(), ui->reorderText->height()));
	ui->reorderText->setHtml("<font size=75><center><br><br><br><br><br>To change to order of the photos on the canvas, perform the select gesture to select one of the photos. "
		"<br><br>The selected photo will move to the top layer and appear on top of all other photos.</font></center>");

	QState *resizeState = new QState(machine);
	hideIndex(resizeState);
	hideHelpContent(resizeState);
	resizeState->assignProperty(ui->resizeText, "geometry", QRect(180, 140, ui->resizeText->width(), ui->resizeText->height()));
	ui->resizeText->setHtml("<font size=75><center><br><br><br><br><br>To resize a photo, select the photo with both cursors. <br><br>To upscale the photo, increase the distance "
		"between the two cursors. To downscale the photo, decrease the distance between the two cursors. <br><br>To exit "
		"resize mode, deselect the photo.</font></center>");

	QState *rotateState = new QState(machine);
	hideIndex(rotateState);
	hideHelpContent(rotateState);
	rotateState->assignProperty(ui->rotateText, "geometry", QRect(180, 140, ui->rotateText->width(), ui->rotateText->height()));
	ui->rotateText->setHtml("To rotate a photo, select the photo with both cursors. Rotate your hands about each other as if "
		"you are using a steering wheel. To exit "
		"rotate mode, deselect the photo.");

	QState *saveState = new QState(machine);
	hideIndex(saveState);
	hideHelpContent(saveState);
	saveState->assignProperty(ui->saveText, "geometry", QRect(180, 140, ui->saveText->width(), ui->saveText->height()));
	ui->saveText->setHtml("<font size=75><center><br><br><br><br><br>To save your FotoKinect project, select the <b>Save</b> button from the menu.  <br><br> "
		"Your project will be saved to "
		"<b>C:\\Users\\Public\\Pictures\\FotoKinect\\Projects</b>, with the "
		"filename as the current timestamp. <br><br>You can edit this project again later by loading it to the canvas.</font></center>");

	QState *skewState = new QState(machine);
	hideIndex(skewState);
	hideHelpContent(skewState);
	skewState->assignProperty(ui->skewText, "geometry", QRect(180, 140, ui->skewText->width(), ui->skewText->height()));
	ui->skewText->setHtml("<font size=75><center><br><br><br><br><br>To skew a photo, select two opposite edges of the photo (the left and right edges, or the top and bottom edges. "
		"<br><br>Push and pull your hands to skew the photo in that direction.  <br><br>To exit skew mode, deselect the photo.</font></center>");


	QState *topicState = new QState(machine);
	topicState->assignProperty(ui->background, "geometry",QRect(420, 330, ui->background->width(), ui->background->height()));
	topicState->assignProperty(ui->crop, "geometry",QRect(180, 340, ui->crop->width(), ui->crop->height()));
	topicState->assignProperty(ui->erase, "geometry",QRect(880, 340,ui->erase->width(), ui->erase->height()));
	topicState->assignProperty(ui->export_2, "geometry",QRect(580, 150, ui->export_2->width(), ui->export_2->height()));
	topicState->assignProperty(ui->flipButton, "geometry",QRect(340, 540, ui->flipButton->width(), ui->flipButton->height()));
	topicState->assignProperty(ui->import_2, "geometry",QRect(280, 150, ui->import_2->width(), ui->import_2->height()));
	topicState->assignProperty(ui->load, "geometry",QRect(510, 230, ui->load->width(), ui->load->height()));
	topicState->assignProperty(ui->moveButton, "geometry",QRect(440, 450, ui->moveButton->width(), ui->moveButton->height()));
	topicState->assignProperty(ui->reorder, "geometry",QRect(630, 450, ui->reorder->width(), ui->reorder->height()));
	topicState->assignProperty(ui->resizeButton, "geometry",QRect(530, 540, ui->resizeButton->width(), ui->resizeButton->height()));
	topicState->assignProperty(ui->rotateButton, "geometry",QRect(760, 540, ui->rotateButton->width(), ui->rotateButton->height()));
	topicState->assignProperty(ui->save, "geometry",QRect(760, 240,ui->save->width(), ui->save->height()));
	topicState->assignProperty(ui->skew, "geometry",QRect(250, 450, ui->skew->width(), ui->skew->height()));
	hideHelpContent(topicState);

	QSignalTransition *tr_intro_topic = introState->addTransition(ui->skiptotopic,
		SIGNAL(clicked()), topicState);

	QSignalTransition *tr_topic_intro = topicState->addTransition(ui->intro,
		SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_background = topicState->addTransition(ui->background, SIGNAL(clicked()), backgroundState);
	QSignalTransition *tr_background_topic = backgroundState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_background = introState->addTransition(ui->background, SIGNAL(clicked()), backgroundState);
	QSignalTransition *tr_background_intro = backgroundState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_crop = topicState->addTransition(ui->crop, SIGNAL(clicked()), cropState);
	QSignalTransition *tr_crop_topic = cropState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_crop = introState->addTransition(ui->crop, SIGNAL(clicked()), cropState);
	QSignalTransition *tr_crop_intro = cropState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_erase = topicState->addTransition(ui->erase, SIGNAL(clicked()), eraseState);
	QSignalTransition *tr_erase_topic = eraseState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_erase = introState->addTransition(ui->erase, SIGNAL(clicked()), eraseState);
	QSignalTransition *tr_erase_intro = eraseState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_export = topicState->addTransition(ui->export_2, SIGNAL(clicked()), exportState);
	QSignalTransition *tr_export_topic = exportState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_export = introState->addTransition(ui->export_2, SIGNAL(clicked()), exportState);
	QSignalTransition *tr_export_intro = exportState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_flip = topicState->addTransition(ui->flipButton, SIGNAL(clicked()), flipState);
	QSignalTransition *tr_flip_topic = flipState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_flip = introState->addTransition(ui->flipButton, SIGNAL(clicked()), flipState);
	QSignalTransition *tr_flip_intro = flipState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_import = topicState->addTransition(ui->import_2, SIGNAL(clicked()), importState);
	QSignalTransition *tr_import_topic = importState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_import = introState->addTransition(ui->import_2, SIGNAL(clicked()), importState);
	QSignalTransition *tr_import_intro = importState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_load = topicState->addTransition(ui->load, SIGNAL(clicked()), loadState);
	QSignalTransition *tr_load_topic = loadState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_load = introState->addTransition(ui->load, SIGNAL(clicked()), loadState);
	QSignalTransition *tr_load_intro = loadState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_reorder = topicState->addTransition(ui->reorder, SIGNAL(clicked()), reorderState);
	QSignalTransition *tr_reorder_topic = reorderState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_reorder = introState->addTransition(ui->reorder, SIGNAL(clicked()), reorderState);
	QSignalTransition *tr_reorder_intro = reorderState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_move = topicState->addTransition(ui->moveButton, SIGNAL(clicked()), moveState);
	QSignalTransition *tr_move_topic = moveState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_move = introState->addTransition(ui->moveButton, SIGNAL(clicked()), moveState);
	QSignalTransition *tr_move_intro = moveState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_resize = topicState->addTransition(ui->resizeButton, SIGNAL(clicked()), resizeState);
	QSignalTransition *tr_resize_topic = resizeState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_resize = introState->addTransition(ui->resizeButton, SIGNAL(clicked()), resizeState);
	QSignalTransition *tr_resize_intro = resizeState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_rotate = topicState->addTransition(ui->rotateButton, SIGNAL(clicked()), rotateState);
	QSignalTransition *tr_rotate_topic = rotateState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_rotate = introState->addTransition(ui->rotateButton, SIGNAL(clicked()), rotateState);
	QSignalTransition *tr_rotate_intro = rotateState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_save = topicState->addTransition(ui->save, SIGNAL(clicked()), saveState);
	QSignalTransition *tr_save_topic = saveState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_save = introState->addTransition(ui->save, SIGNAL(clicked()), saveState);
	QSignalTransition *tr_save_intro = saveState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	QSignalTransition *tr_topic_skew = topicState->addTransition(ui->skew, SIGNAL(clicked()), skewState);
	QSignalTransition *tr_skew_topic = skewState->addTransition(ui->skiptotopic, SIGNAL(clicked()), topicState);
	QSignalTransition *tr_intro_skew = introState->addTransition(ui->skew, SIGNAL(clicked()), skewState);
	QSignalTransition *tr_skew_intro = skewState->addTransition(ui->intro, SIGNAL(clicked()), introState);

	machine->setInitialState(introState);
	machine->start();

	this->hideFrames();

}

void HelpWidget::hideIndex(QState * state){
	state->assignProperty(ui->background, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->crop, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->erase, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->export_2, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->flipButton, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->import_2, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->load, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->moveButton, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->reorder, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->resizeButton, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->rotateButton, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->save, "geometry",QRect(0, this->height()+100, 250, 250));
	state->assignProperty(ui->skew, "geometry",QRect(0, this->height()+100, 250, 250));
}

void HelpWidget::hideFrames(){
	ui->introText->setFrameStyle( QFrame::NoFrame );
	ui->backgroundText->setFrameStyle( QFrame::NoFrame );
	ui->cropText->setFrameStyle( QFrame::NoFrame );
	ui->eraseText->setFrameStyle( QFrame::NoFrame );
	ui->exportText->setFrameStyle( QFrame::NoFrame );
	ui->flipText->setFrameStyle( QFrame::NoFrame );
	ui->importText->setFrameStyle( QFrame::NoFrame );
	ui->loadText->setFrameStyle( QFrame::NoFrame );
	ui->moveText->setFrameStyle( QFrame::NoFrame );
	ui->reorderText->setFrameStyle( QFrame::NoFrame );
	ui->resizeText->setFrameStyle( QFrame::NoFrame );
	ui->rotateText->setFrameStyle( QFrame::NoFrame );
	ui->saveText->setFrameStyle( QFrame::NoFrame );
	ui->skewText->setFrameStyle( QFrame::NoFrame );
}

QToolButton * HelpWidget::getIntroButton(){
	return ui->intro;
}

QToolButton * HelpWidget::getTopicsButton(){
	return ui->skiptotopic;
}

QToolButton * HelpWidget::getFlipButton(){
	return ui->flipButton;
}

QToolButton * HelpWidget::getRotateButton(){
	return ui->rotateButton;
}

QToolButton * HelpWidget::getImportButton(){
	return ui->import_2;
}

QToolButton * HelpWidget::getLoadButton(){
	return ui->load;
}

QToolButton * HelpWidget::getExportButton(){
	return ui->export_2;
}

QToolButton * HelpWidget::getSaveButton(){
	return ui->save;
}

QToolButton * HelpWidget::getCropButton(){
	return ui->crop;
}

QToolButton * HelpWidget::getBackgroundButton(){
	return ui->background;
}

QToolButton * HelpWidget::getEraseButton(){
	return ui->erase;
}

QToolButton * HelpWidget::getSkewButton(){
	return ui->skew;
}

QToolButton * HelpWidget::getResizeButton(){
	return ui->resizeButton;
}

QToolButton * HelpWidget::getReorderButton(){
	return ui->reorder;
}

QToolButton * HelpWidget::getMoveButton(){
	return ui->moveButton;
}

void HelpWidget::hideHelpContent(QState * state){
	state->assignProperty(ui->introText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->backgroundText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->cropText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->eraseText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->exportText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->flipText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->importText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->loadText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->moveText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->reorderText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->resizeText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->rotateText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->saveText, "geometry", QRect(0, this->height(), 250, 250));
	state->assignProperty(ui->skewText, "geometry", QRect(0, this->height(), 250, 250));
}

HelpWidget::~HelpWidget()
{
	delete ui;
}

